package com.linkhub.oms.admin.modules.whitelist.service.impl;

import com.linkhub.oms.admin.modules.whitelist.service.WhiteListBizService;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mpservice.IWhiteListService;
import com.linkhub.oms.entity.WhiteListEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: WhiteListBizService
 * @Author: echo
 * @Date: 2024/3/8 17:09
 * @Version: 1.0
 */
@Slf4j
@Service
public class WhiteListBizServiceImpl implements WhiteListBizService {

    @Resource
    private IWhiteListService whiteListService;


    @Override
    public boolean exists(String email) {
        return whiteListService.exists(email);
    }

    @Override
    public void addUserToWhiteList(String email) {
        if (this.exists(email)) {
            return;
        }

        WhiteListEntity whiteListEntity = create(email);

        whiteListService.save(whiteListEntity);
    }

    @Override
    public void addBatchToWhiteList(Collection<String> emails) {
        List<WhiteListEntity> whiteLists = QtStreamUtil.map(emails, this::create);
        whiteListService.saveBatch(whiteLists);
    }



    @Override
    public Set<String> listExistsEmails(Collection<String> emails) {
        List<WhiteListEntity> whiteList = whiteListService.listByEmails(emails);
        return QtStreamUtil.extractFieldCollection(whiteList, WhiteListEntity::getEmail, HashSet::new);
    }


    private WhiteListEntity create(String email) {
        WhiteListEntity whiteListEntity = new WhiteListEntity();
        whiteListEntity.setEmail(email);
        whiteListEntity.setCreatedTime(LocalDateTime.now());
        whiteListEntity.setUpdateTime(LocalDateTime.now());
        return whiteListEntity;
    }
}
